package com.example.common.fraction;

import com.example.common.model.JavaCourseModel;
import com.example.common.model.bean.JavaCourse;
import com.example.commonlib.base.presenter.SuperPresenter;
import com.example.commonlib.net.ServiceResponse;
import ohos.aafwk.content.Intent;
import ohos.hiviewdfx.HiLog;
import ohos.hiviewdfx.HiLogLabel;

public class TextListPresenter extends SuperPresenter<TextListFraction> {
    private static final HiLogLabel LABEL = new HiLogLabel(HiLog.LOG_APP, 0x00201, "test001");

    private int mPage = 0;

    @Override
    public void onCreate(Intent b) {
        HiLog.error(LABEL, "*****************TextListPresenter onCreate******************");
        getData(true);
    }

    public void loadMore() {
        getData(false);
    }

    public void refreshData() {
        getData(true);
    }

    public void getData(final boolean isRefresh) {
        if (isRefresh) {
            mPage = 0;
        } else {
            mPage++;
        }
        ServiceResponse<JavaCourse[]> serviceResponse = new ServiceResponse<JavaCourse[]>() {
            @Override
            public void onNext(JavaCourse[] javaCourses) {
                HiLog.error(LABEL, "*****************TextListPresenter onNext******************");

                if (javaCourses.length == 0 && mPage == 0) {
                    getView().showEmpty();
                } else {
                    getView().showContent();
                }
            }

            @Override
            public void onError(Throwable e) {
                super.onError(e);
                HiLog.error(LABEL, "*****************TextListPresenter onError******************");
                getView().showError();
            }
        };
        JavaCourseModel.getInstance().getTextJavaCourseList(mPage, serviceResponse);
        putDisposable(serviceResponse);
    }
}
